//
//  LS_TabBindViewHelp.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/10.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_TabBindViewHelp.h"
#import "LS_Reactiveview.h"
#import "LS_ActivitysModel.h"
@interface LS_TabBindViewHelp()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)RACCommand *didSelectCommand;
@property(nonatomic,strong)id viewModel;
@property(nonatomic,strong)NSMutableArray *cellIdentifierArr;

//数据赋值
@property(nonatomic,strong)NSArray *ls_newsArr;
@property(nonatomic,strong)NSArray *ls_soreItemArr;
@property(nonatomic,strong)NSArray<LS_ActivitysModel *> *ls_advertisesItem;
@property(nonatomic,strong)NSDictionary *ls_advertise_pic;
@end
@implementation LS_TabBindViewHelp
-(instancetype)initWithTableview:(UITableView *)tableView SourceSignal:(RACSignal *)source selectCommand:(RACCommand *)didSelectionCommand templateCell:(NSArray *)templecesss withViewModel:(id)viewModel
{
    if (self = [super init]) {
        self.cellIdentifierArr = @[].mutableCopy;
        _ls_newsArr = @[];
        _ls_soreItemArr = @[];
        _ls_advertise_pic = @{};
        _tableview = tableView;
        _viewModel = viewModel;
        tableView.delegate = self;
        tableView.dataSource = self;
        @weakify(self)
        [source subscribeNext:^(id result) {
            @strongify(self)
            NSLog(@"x-----------%@",result);
            
           self.ls_newsArr = [NSArray arrayWithArray:result[FoundNewskey]];
           self.ls_soreItemArr = [NSArray arrayWithArray:result[FoundSorekey]];
           self.ls_advertisesItem =[NSArray arrayWithArray:result[FoundActivitykey]] ;
           self.ls_advertise_pic = result[FoundAdd_activity_pic];
           [self.tableview reloadData];
        }];
        
        //注册cell
        for (NSString *tempCell in templecesss) {
            Class class = NSClassFromString(tempCell);
            [self.tableview registerClass:class forCellReuseIdentifier:tempCell];
            [self.cellIdentifierArr addObject:tempCell];
        }
       
    }return self;
}
+(instancetype)bindingHelperForTableView:(UITableView *)tableView sourceSignal:(RACSignal *)source selectCommand:(RACCommand *)didSelctionCommad templateCell:(NSArray *)templateCells withViewModel:(id)viewModel
{
    return [[LS_TabBindViewHelp alloc]initWithTableview:tableView SourceSignal:source selectCommand:didSelctionCommad templateCell:templateCells withViewModel:viewModel];
    
}
#pragma mark -
#pragma mark --tableview delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.ls_soreItemArr.count == 0 ? 0 : 2 + self.ls_advertisesItem.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return  ScreenWidth/4*1.2*2;
    }else if (indexPath.section == 1){
        return [self.ls_advertise_pic[@"imgheight"] floatValue];
    }else{
        LS_ActivitysModel *model = self.ls_advertisesItem[indexPath.section-2];
        return model.rowHeight;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
//    if (indexPath.section == 0) {
          id<LS_Reactiveview >  cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifierArr[indexPath.section]];
         [cell bindViewModel:self.viewModel withParams:@{DataIndex:@(indexPath.row)}];
         return (UITableViewCell *)cell;
//    }else if (indexPath.section == 1){
//          id<LS_Reactiveview >  cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifierArr[indexPath.section]];
//         [cell bindViewModel:self.ls_advertise_pic withParams:@{DataIndex:@(indexPath.row)}];
//         return (UITableViewCell *)cell;
//    }else{
//          id<LS_Reactiveview >  cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifierArr[2]];
//
//       [cell bindViewModel:self.ls_advertisesItem[indexPath.section-2] withParams:@{DataIndex:@(indexPath.section-2)}];
//         return (UITableViewCell *)cell;
//    }
   
   
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        
    }else if (indexPath.section == 1){
    }else{
       [self.didSelectCommand execute:self.ls_advertisesItem[indexPath.row]];
    }
    if ([self.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
        [self.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
}
#pragma mark = UITableViewDelegate forwarding

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
        [self.delegate scrollViewDidScroll:scrollView];
    }
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    if ([self.delegate respondsToSelector:aSelector]) {
        return YES;
    }
    return [super respondsToSelector:aSelector];
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
    if ([self.delegate respondsToSelector:aSelector]) {
        return self.delegate;
    }
    return [super forwardingTargetForSelector:aSelector];
}


@end
